﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace Tingjian.gas.service.BaseReponse
{
    public class BaseResultModel
    {
        [JsonPropertyName("timestamp")]
        [JsonProperty("timestamp")]
        public long Timestamp => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

        [JsonPropertyName("code")]
        [JsonProperty("code")]
        public string Code { get; set; }

        [JsonPropertyName("msg")]
        [JsonProperty("msg")]
        public string Message { get; set; }

        #region Succeed

        public static BaseResultModel<T> Succeed<T>(string code = ReturnCode.Success, T data = default)
        {
            return new BaseResultModel<T>
            {
                Code = code,
                Message = data == null ? "未获取到数据！" : "获取数据成功！",
                Data = data
            };
        }

        public static BaseResultModel<T> Succeed<T>(string code, string message, T data)
        {
            return new BaseResultModel<T>
            {
                Code = code,
                Message = message ?? (data == null ? "未获取到数据！" : "获取数据成功！"),
                Data = data
            };
        }

        #endregion

        #region Failed

        public static BaseResultModel Failed(string code, string message)
        {
            return new BaseResultModel
            {
                Code = code,
                Message = message ?? "获取数据失败！"
            };
        }

        public static BaseResultModel<T> Failed<T>(string code, string message)
        {
            return new BaseResultModel<T>
            {
                Code = code,
                Message = message ?? "获取数据失败！"
            };
        }

        public static BaseResultModel<T> Failed<T>(string code, string message, T data)
        {
            return new BaseResultModel<T>
            {
                Code = code,
                Message = message ?? "获取数据失败！",
                Data = data
            };
        }

        #endregion
    }


    public class BaseResultModel<T> : BaseResultModel
    {
        [JsonPropertyName("data")]
        [JsonProperty("data")]
        public T Data { get; set; }
    }

    public static class ReturnCode
    {
        //0,1,2,3开头的code，均为网关内置code
        //0000 正常  
        public const string Success = "0000";

        //1003 参数错误
        public const string ParamError = "1003";

        //1005 操作权限校验失败
        public const string OperatePermisionError = "1005";

        //1006 数据权限校验失败
        public const string DataPermisionError = "1006";

        //2001 处理失败
        public const string Failure = "2001";

        //3001 业务处理失败
        public const string OperationError = "3001";


        //4以后开头的code，均为自定义dode
        //4001 数据不存在
        public const string DataNotExists = "4001";
    }
}
